import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/model/other/protocol_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/custom_logger.dart';
import 'package:flutter_changdian_football/utils/share_util.dart';
import 'package:flutter_changdian_football/widget/sheet/share_icon_sheet.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:sharesdk_plugin/sharesdk_defines.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'package:sharesdk_plugin/sharesdk_map.dart';
import 'package:sharesdk_plugin/sharesdk_plugin.dart';

class ContactCustomerServiceController extends GetxController {

  @override
  void onInit() {
    super.onInit();

    initData();
  }

  ProtocolModel? protocolModel;

  initData() async {
    try {
      EasyLoading.show();
      var resp = await ReqOther.getProtocol(CustomConstant.CUSTOMER_SERVICE);

      if (resp.data['code'] == '200') {
        protocolModel = ProtocolModel.fromJson(resp.data['data']);
        update();
      } else {
        CustomToast.showTextToast(resp.data['msg']);
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 保存客服图片
  saveNetworkImage() async {
    if (protocolModel?.url == null) {
      return ;
    }
    try {
      EasyLoading.show();
      var response = await Dio().get(
        protocolModel?.url ?? '',
        options: Options(responseType: ResponseType.bytes)
      );
      final result = await ImageGallerySaver.saveImage(
        Uint8List.fromList(response.data),
        quality: 60,
        name: "hello"
      );
      if (result['isSuccess'] == true) {
        CustomToast.showTextToast('保存成功');
      } else {
        CustomToast.showTextToast('保存失败');
      }
    } catch (e) {
      CustomToast.showTextToast('保存失败');
      EasyLoading.dismiss();
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 分享
  shareQrCode() {
    if (protocolModel?.url == null) {
      return ;
    }

    ShareIconSheet.shareIcon(
      null, 
      null, 
      protocolModel?.url, 
      null,
      wxTap: () {
        ShareUtil.shareImg(title: '分享客服', description: '分享客服',  image: protocolModel?.url, shareType: "0");
      },
      pyqTap: () {
        ShareUtil.shareImg(title: '分享客服', description: '分享客服',  image: protocolModel?.url, shareType: "1");
      },
      qqTap: () {
        ShareUtil.shareImg(title: '分享客服', description: '分享客服',  image: protocolModel?.url, shareType: "2");
      },
      wbTap: () {
        ShareUtil.shareImg(title: '分享客服', description: '分享客服',  image: protocolModel?.url, shareType: "3");
      },
    );
  }

}